import 'package:flutter/material.dart';

/// 用户个人资料模型
class UserProfileModel {
  final String id;
  final String username;
  final String? nickname;
  final String? avatarUrl;
  final String? signature;
  final String? gender;
  final DateTime? birthDate;
  final String? email;
  final String? phone;
  final List<UserTagModel>? skillTags;
  final DateTime? createTime;
  final DateTime? updateTime;

  UserProfileModel({
    required this.id,
    required this.username,
    this.nickname,
    this.avatarUrl,
    this.signature,
    this.gender,
    this.birthDate,
    this.email,
    this.phone,
    this.skillTags,
    this.createTime,
    this.updateTime,
  });

  factory UserProfileModel.fromJson(Map<String, dynamic> json) {
    return UserProfileModel(
      id: json['id']?.toString() ?? '',
      username: json['username'] ?? '',
      nickname: json['nickname'],
      avatarUrl: json['avatarUrl'],
      signature: json['signature'],
      gender: json['gender'],
      birthDate: json['birthDate'] != null
          ? DateTime.tryParse(json['birthDate'])
          : null,
      email: json['email'],
      phone: json['phone'],
      skillTags: json['skillTags'] != null
          ? (json['skillTags'] as List<dynamic>)
              .map((tag) => UserTagModel.fromJson(tag))
              .toList()
          : null,
      createTime: json['createTime'] != null
          ? DateTime.tryParse(json['createTime'])
          : null,
      updateTime: json['updateTime'] != null
          ? DateTime.tryParse(json['updateTime'])
          : null,
    );
  }

  Map<String, dynamic> toJson() {
    return {
      'id': id,
      'username': username,
      'nickname': nickname,
      'avatarUrl': avatarUrl,
      'signature': signature,
      'gender': gender,
      'birthDate': birthDate?.toIso8601String(),
      'email': email,
      'phone': phone,
      'skillTags': skillTags?.map((tag) => tag.toJson()).toList(),
      'createTime': createTime?.toIso8601String(),
      'updateTime': updateTime?.toIso8601String(),
    };
  }
}

/// 用户标签模型
class UserTagModel {
  final String id;
  final String userId;
  final String tagName;
  final String tagType;
  final String tagSource;
  final int tagWeight;

  UserTagModel({
    required this.id,
    required this.userId,
    required this.tagName,
    required this.tagType,
    required this.tagSource,
    required this.tagWeight,
  });

  factory UserTagModel.fromJson(Map<String, dynamic> json) {
    return UserTagModel(
      id: json['id']?.toString() ?? '',
      userId: json['userId']?.toString() ?? '',
      tagName: json['tagName'] ?? '',
      tagType: json['tagType'] ?? '',
      tagSource: json['tagSource'] ?? '',
      tagWeight: json['tagWeight'] ?? 1,
    );
  }

  Map<String, dynamic> toJson() {
    return {
      'id': id,
      'userId': userId,
      'tagName': tagName,
      'tagType': tagType,
      'tagSource': tagSource,
      'tagWeight': tagWeight,
    };
  }
}

/// 更新个人资料请求模型
class UpdateUserProfileDTO {
  final String nickname;
  final String? avatarUrl;
  final String? signature;
  final String? gender;
  final String? birthDate;
  final String? email;
  final String? phone;
  final List<String>? skillTags;

  UpdateUserProfileDTO({
    required this.nickname,
    this.avatarUrl,
    this.signature,
    this.gender,
    this.birthDate,
    this.email,
    this.phone,
    this.skillTags,
  });

  Map<String, dynamic> toJson() {
    return {
      'nickname': nickname,
      'avatarUrl': avatarUrl,
      'signature': signature,
      'gender': gender,
      'birthDate': birthDate,
      'email': email,
      'phone': phone,
      'skillTags': skillTags,
    };
  }
}

